package org.acme.sync;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
public class SysController {

    private Map<String, SyncResponseWrap> map = new ConcurrentHashMap();

    /****
     * 获取当前时间
     * @return
     */
    @RequestMapping(value = "getTime")
    private SyncResponse getTime(String id) {
        SyncRequest request = new SyncRequest(id);
        this.map.put(id, new SyncResponseWrap(id, request));
        return this.map.get(id).getSyncResponse();
    }

    /****
     * 设置结果
     * @param id
     * @return
     */
    @RequestMapping(value = "setTime")
    private String setTime(String id) {
        SyncResponseWrap syncResponseWrap = this.map.remove(id);
        if (syncResponseWrap != null) {
            syncResponseWrap.setSyncResponse(new SyncResponse(id));
        }
        return id;
    }
}
